package cn.blnp.net.flowable.boot.localize.dynamic.modify;

import cn.blnp.net.flowable.boot.service.TaskRecallService;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.impl.RepositoryServiceImpl;
import org.flowable.engine.impl.persistence.deploy.DeploymentManager;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * <h3>动态修改流程定义元素用例</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/20 15:26
 */
@Slf4j
@SpringBootTest
public class DynamicModifyFlowTest {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;
    @Resource
    private TaskRecallService taskRecallService;
    @Resource
    private DynamicBpmnService dynamicBpmnService;
    @Resource
    private ProcessEngineConfiguration processEngineConfiguration;
    @Resource
    private ProcessEngine processEngine;

    /**
     *  <h1>动态修改流程定义元素</h1>
     *  <ul>
     *      <li><b>流程定义文件：</b>{@link flow/dynamicBpmnServiceProcess.bpmn20.xml}</li>
     *      <li>
     *          <b>流程预览图：</b></br>
     *          <img src="https://cdn4.winhlb.com/2025/06/20/68550ec198808.png" >
     *      </li>
     *  </ul>
     **/
    @Test
    public void test() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("flow/dynamicBpmnServiceProcess.bpmn20.xml")
                .name("动态修改流程定义元素案例")
                .deploy();
        log.info("部署ID：{}", deploy.getId());
        //流程定义信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        log.info("流程定义ID：{}", processDefinition.getId());
        //启动流程实例
        ProcessInstance procInst = runtimeService.startProcessInstanceById(processDefinition.getId());
        log.info("流程实例ID：{}", procInst.getId());

        TaskQuery taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst.getId());
        Task firstTask = taskQuery.singleResult();
        log.info("修改前第一个节点的名称：{}", firstTask.getName());
        taskService.complete(firstTask.getId());
        Task secondTask = taskQuery.singleResult();
        log.info("第二个节点的名称：{}，办理人：{}", secondTask.getName(), secondTask.getAssignee());
        //查询ProcessDefinitionInfoCacheObject的infoNode值
        ObjectNode infoNode = dynamicBpmnService.getProcessDefinitionInfo(procInst.getProcessDefinitionId());
        //修改第一个节点的名称
        dynamicBpmnService.changeUserTaskName("application", "请假申请", infoNode);
        //修改第二个节点的名称和办理人
        dynamicBpmnService.changeUserTaskName("approval", "上级审批", infoNode);
        dynamicBpmnService.changeUserTaskAssignee("approval", "大乔", infoNode);
        //保存动态修改后的ProcessDefinitionInfoCacheObject
        dynamicBpmnService.saveProcessDefinitionInfo(procInst.getProcessDefinitionId(), infoNode);

        //启动修改后的流程
        ProcessInstance procInst2 = runtimeService.startProcessInstanceByKey("dynamicBpmnServiceProcess");
        taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst2.getId());
        firstTask = taskQuery.singleResult();
        log.info("修改后第一个节点的名称：{}", firstTask.getName());
        taskService.complete(firstTask.getId());
        secondTask = taskQuery.singleResult();
        log.info("第二个节点的名称：{}，办理人：{}", secondTask.getName(), secondTask.getAssignee());

        /**
         *  演示结果：
         **/
    }
}
